<template>
    <view class="content">
        <view class="logo">
            <img class="logoimg" src="@/static/watch.png"></img>
        </view>
        <view class="btn-row">
            <button class="vxBtn" type="default" @tap="loginByWeixin">微信一键登录</button>
            <button type="default" class="mbBtn" @tap="phoneLogin">手机号登录</button>
        </view>
        <view class="action-row">
            <view @click="changeType">{{ pageTitle === '家长登录' ? '运营商登录' : '家长登录' }}</view>
        </view>
        <view class="agree">
            <label class="radio">
                <radio value="r1" style="transform:scale(0.5)" />
            </label>我已阅读并同意<span>迪沃用户服务协议、隐私政策</span>
        </view>
    </view>
</template>

<script>


export default {
    components: {

    },
    data() {
        return {
            username: "",
            password: "",
            confirmPassword: "",
            pageTitle: '',
            platform: uni.getSystemInfoSync().platform,
            loginType: 0,
            loginTypeList: ['免密登录', '密码登录'],
            mobile: '',
            code: '',
            providerList: [],
            hasProvider: false,
            username: '',
            password: '',
            positionTop: 0,
            isDevtools: false,
            codeDuration: 0,
            loginBtnLoading: false,
            hasAppleLogin: false,
            needCaptcha: uni.getStorageSync('uni-needCaptcha'),
            captchaing: false,
            captchaBase64: '',
            captchaText: ''
        };
    },
    onLoad() {
        console.log(123123, uni)

        var pages = getCurrentPages()
        console.log(pages)

        var page = pages[pages.length - 1]
        var title = page.$holder.navigationBarTitleText
        console.log(pages)
        this.pageTitle = title

    },
    methods: {
        register() { },
        initProvider() {
            const filters = ['weixin', 'qq', 'sinaweibo', 'univerify'];
            uni.getProvider({
                service: 'oauth',
                success: (res) => {
                    if (res.provider && res.provider.length) {
                        if (res.provider.indexOf('apple') !== -1) {
                            this.hasAppleLogin = true;
                        }
                        for (let i = 0; i < res.provider.length; i++) {
                            const curProvider = res.provider[i];
                            if (~filters.indexOf(curProvider)) {
                                this.providerList.push({
                                    value: curProvider,
                                    image: '../../static/img/' + curProvider + '.png'
                                });
                            }
                        }
                        this.hasProvider = true;
                    }
                },
                fail: (err) => {
                    console.error('获取服务供应商失败：' + JSON.stringify(err));
                }
            });
        },
        initPosition() {
            /**
             * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
             * 反向使用 top 进行定位，可以避免此问题。
             */
            this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
        },
        changeType() {
            uni.setNavigationBarTitle({
                title: this.pageTitle === '家长登录' ? '运营商登录' : '家长登录'
            });
            this.pageTitle = this.pageTitle === '家长登录' ? '运营商登录' : this.pageTitle
        },
        loginByWeixin(value) {
            this.oauth(value).then((code) => {
                return uniCloud.callFunction({
                    name: 'user-center',
                    data: {
                        action: 'loginByWeixin',
                        params: {
                            code,
                        }
                    }
                })
            }).then((res) => {
                if (res.result.code === 0) {

                    uni.setStorageSync('uni_id_token', res.result.token)
                    uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
                    uni.setStorageSync('login_type', 'online')
                    uni.setStorageSync('username', '微信用户')
                    this.toMain('微信用户')
                }
            }).catch((e) => {
                console.error(e)
                uni.showModal({
                    showCancel: false,
                    content: '微信登录失败，请稍后再试'
                })
            })
        },
        phoneLogin() {
            //在起始页面跳转到test.vue页面并传递参数
            uni.navigateTo({
                url: '/pages/login/login'
            });

        }
    },
    onReady() {
        this.initPosition();
        this.initProvider();
        // #ifdef MP-WEIXIN
        this.isDevtools = uni.getSystemInfoSync().platform === 'devtools';
        // #endif
    }

};
</script>

<style lang="scss" scoped>
.content {
    padding: 0 45rpx;
}

.logo {
    margin-top: 79rpx;
    width: 160rpx;
    height: 160rpx;
    border-radius: 10px;
    margin-left: 50%;
    transform: translateX(-50%);
    margin-bottom: 99.5rpx;

}

.logoimg {
    width: 160rpx;
    height: 160rpx;
}

.vxBtn {
    margin-bottom: 10.5rpx;
    background-color: #13AC10;
    color: #fff;
}

.mbBtn {
    background-color: #fff;
    margin-bottom: 13.5rpx;
}

.action-row {
    display: flex;
    flex-direction: row;
    justify-content: center;
    margin-bottom: 48.5rpx;
}

.action-row view {
    color: #007aff;
    padding: 0 10px;
}

.agree {
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;

    span {
        color: #007aff;
    }
}
</style>
